package cn.com.bluemoon.order.print.po;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;

import org.xhtmlrenderer.css.style.CalculatedStyle;
import org.xhtmlrenderer.layout.Layer;
import org.xhtmlrenderer.render.PageBox;
import org.xhtmlrenderer.render.RenderingContext;
import org.xhtmlrenderer.simple.XHTMLPanel;

public class OrderXHTMLPanel extends XHTMLPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void paintPage(Graphics2D g, int pageNo) {
		Layer root = getRootLayer();

		if (root == null) {
			throw new RuntimeException("Document needs layout");
		}

		if (pageNo < 0 || pageNo >= root.getPages().size()) {
			throw new IllegalArgumentException(
					"Page " + pageNo + " is not between 0 " + "and " + root.getPages().size());
		}

		RenderingContext c = newRenderingContext(g);

		PageBox page = (PageBox) root.getPages().get(pageNo);
		c.setPageCount(root.getPages().size());
		c.setPage(pageNo, page);

		page.paintBackground(c, 0, Layer.PAGED_MODE_PRINT);
		page.paintMarginAreas(c, 0, Layer.PAGED_MODE_PRINT);
		page.paintBorder(c, 0, Layer.PAGED_MODE_PRINT);

		Shape working = g.getClip();
       
		Rectangle content = page.getPrintClippingBounds(c);
		g.clip(content);

		 int top = -page.getPaintingTop() + page.getMarginBorderPadding(c,
		 CalculatedStyle.TOP);

		 int left = page.getMarginBorderPadding(c, CalculatedStyle.LEFT);
		//int top = 0;
	    //int left = 0;

		 System.out.println(top+","+left);
		g.translate(left, top);
		root.paint(c);
		g.translate(-left, -top);

		g.setClip(working);
	}
	
	
	

}
